package com.baseframe.topic.mapper;

import com.baseframe.topic.entity.SnsTestTopicItem;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baseframe.topic.entity.vo.SnsTestTopicItemOptionVO;
import com.baseframe.topic.entity.vo.SnsTestTopicItemVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**

* @description 针对表【sns_test_topic_item(测验题目表)】的数据库操作Mapper
* @createDate 2023-06-29 23:12:25
* @Entity com.baseframe.topic.entity.SnsTestTopicItem
*/
public interface SnsTestTopicItemMapper extends BaseMapper<SnsTestTopicItem> {

    @Select("select SUM(stti.award) as total_score from sns_test_topic_item as stti where is_deleted = 0 and stti.topic_id = #{topicId}")
    Integer totalScore(@Param("topicId") Long topicId);

    @Select("select " +
            "stti.*,sttio.name as correct_answer_name" +
            " from sns_test_topic_item_option as sttio left join sns_test_topic_item as stti on sttio.id = stti.correct_answer  where stti.is_deleted = 0 and stti.topic_id = #{topicId} order by stti.sort")
    @Results(

            {       @Result(
                        id = true,
                        property = "id",
                        column = "id"
                    ),
                    @Result(
                            property = "optionList",
                            column = "id",
                            javaType = List.class,
                            many = @Many(select = "getTopicItemOptions")
                    ),
                    @Result(
                            property = "correctAnswerItemName",
                            column = "correct_answer_name"
                    )

            }
    )
    public List<SnsTestTopicItemVO> getTopicItems(@Param("topicId") Long topicId);


    @Select("select * from sns_test_topic_item_option where is_deleted = 0 and topic_item_id = #{topicItemId} order by name")
    public List<SnsTestTopicItemOptionVO> getTopicItemOptions(@Param("topicItemId") Long topicItemId);


}




